package OtherPubs;

import Rout.*;
import com.sun.org.omg.SendingContext.CodeBasePackage.URLSeqHelper;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.*;

public class MobileResearch {

    private static GoBefore browser;
    private static final ArrayList<String> urls = new ArrayList<String>();
    private int attempt;   // how much we'll try for catch events
    private ArrayList<String> newTab;
    private int timeOut;
    private String geo;   
    
    
    //Run chrome adb and chromedriver first:  
    //    execute /home/user/Installs/Android/android-sdk_r24.0.2-linux/android-sdk-linux/platform-tools/./adb start-server 
   
    
    @BeforeMethod
    public void setUp() throws Exception {
        browser = new GoBefore();
        
        urls.add("http://testpagefortesters.tk/temp_pages/72083ac026638c93a7dad1910e50584d.html");
        
        System.out.println("\n Run test: " + getClass().getSimpleName());
        browser.setXpFilename(null);
        browser.setEnableSafeBrow(false);
        browser.setDetctButton(false);
        browser.setChDrvPath("/home/user/Dropbox/WebDriver/lin32/chromedriver");
        browser.setMobile(false);
        browser.setXpButton("/html/body//div//*//a|*//iframe");       
        browser.setUserAg(1);
        setTimeOut(3000);
        setAttempt(3);
        
        
       
        
    }

    @Test //Void Footer Baner
    public void Test() throws Exception {

        for (String url : urls) {

            browser.setCurURL(url);
            System.out.println("\n\nTest case: " + (urls.indexOf(url) + 1));
            
            for (int m = 0; m < attempt; m++) {                
                browser.setUp();

                Thread.sleep(timeOut);
                try {
                    geo = browser.getDriver().findElement(By.id("geo")).getText();
                    System.out.println(geo);
                } catch (Exception e) {
                    attempt++;
                    Down();
                    Test();
                }
                browser.setRepDir(geo + "_" + (urls.indexOf(url) + 1) + "_" + "_");
                System.out.println("\nTest URL is: " + url);
                System.out.println("attempt: " + m);
                Thread.sleep(timeOut);                              
                browser.takeFoto();                           
                System.out.println("!" + browser.getFotoName() + "|thumbnail!");               
                
                Thread.sleep(timeOut);  
                //click on milk
                 
                
                browser.testRun();                                
                              
                
                

                //Get number of open tabs            
                newTab = new ArrayList<String>(browser.getDriver().getWindowHandles());
                if (newTab.size() > 1) {
                    System.out.print("Open new tab.");
                    Thread.sleep(timeOut);
                    System.out.println(" Total open tabs is: " + newTab.size());
                    for (int i = 1; i < newTab.size(); i++) {
//If 2 tabs present, switch to new tab  

                        try {
                            browser.getDriver().switchTo().window(newTab.get(i));                            
                            System.out.println("Switch to tab # " + i);

                            Thread.sleep(timeOut);

                            String NextUrl = browser.getDriver().getCurrentUrl();
                            Thread.sleep(timeOut);
                            browser.takeFoto();

                            System.out.println("Redirect to: " + NextUrl);                            
                            System.out.println("!" + browser.getFotoName() + "|thumbnail!");
                        } catch (Exception e) {
                            System.out.println("exeption in  loop" + e);
                        }
                        if (browser.getDriver().getCurrentUrl() == url) {
                            break;                            
                        }
                    }

                } else {
                browser.takeFoto();
                System.out.println("landing page is : "+browser.getThisURL()+" !" + browser.getFotoName() + "|thumbnail!");
                }

                Thread.sleep(timeOut);
                browser.setRepDir(null);
                browser.getDriver().close();
            }
        }
    }

    @AfterMethod
    public void Down() throws Exception {
        browser.getDriver().quit();

    }

    public int getAttempt() {
        return attempt;
    }

    public void setAttempt(int attempt) {
        this.attempt = attempt;
    }

    public int getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }
    
    public void addURL(String url) {
        urls.add(url);
    }
    
    public void setXpathURL(String xpath) {        
        browser.setXpButton(xpath);
    }

}
